package com.yunkaweilai.android.activity.login;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.github.lazylibrary.util.StringUtils;
import com.google.gson.Gson;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.base.ActivityCollector;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.base.BaseApplication;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.interfaces.DialogMemberPayInterface;
import com.yunkaweilai.android.interfaces.MyDialogInterface;
import com.yunkaweilai.android.model.StoreInfoModel;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.CountDownTimerUtils;
import com.yunkaweilai.android.utils.TitleBuilder;
import com.yunkaweilai.android.utils.Utils;
import com.yunkaweilai.android.view.MyToast;
import com.yunkaweilai.android.view.dialog.MyDialog;
import com.yunkaweilai.android.view.dialog.login.ForgetCodeDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 忘记密码
 * Created by Administrator on 2017/7/22.
 */

public class ZhuXiaoActivity extends BaseActivity implements MyDialogInterface , DialogMemberPayInterface {

    @BindView(R.id.id_edt_zh)
    EditText idEdtZh;
    @BindView(R.id.id_edt_phone)
    EditText idEdtPhone;
    @BindView(R.id.id_btn_next)
    Button idBtnNext;
    @BindView(R.id.id_edt_code)
    EditText idEtYZM;
    @BindView(R.id.id_tv_time)
    TextView idTvTime;
    private CountDownTimerUtils countDownTimerUtils;
    private String UserName;
    private String Phone;
    private String cToken;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhuxiao);
        ButterKnife.bind(this);
        new TitleBuilder(mAc).setTitleText("注销账号")
                .setLeftImage(R.mipmap.ic_go_back)
                .setLeftOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });


        initView();
    }

    private void initView() {
        idEdtZh.setFocusable(false);
        idEdtZh.setFocusableInTouchMode(false);
        idEdtZh.setText(BaseApplication.Zhanghao);
        idEdtPhone.setHint("请输入登录密码");
        idBtnNext.setText("注销");
        countDownTimerUtils = new CountDownTimerUtils(idTvTime, 60000, 1000);

        HttpRequest.postUrl(Api.STORE_INFO)
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(mAc, json);
                        if (checkCode) {
                            Gson gson = new Gson();
                            StoreInfoModel storeInfoModel = gson.fromJson(json, StoreInfoModel.class);
                            UserName = storeInfoModel.getData().getInfo().getUsername();
                            Phone = storeInfoModel.getData().getInfo().getRegist_phone();
                        }

                    }

                    @Override
                    public void onError(Exception e) {
                        MyToast.showShortToast(mAc, e.getMessage());
                    }
                });

    }

    //验证参数是否为空
    private void isRight() {
        if (StringUtils.isEmpty(idEdtZh.getText().toString())) {
            showToast("请输入登录账号");
            return;
        }
        if (StringUtils.isEmpty(idEdtPhone.getText().toString())) {
            showToast("请输入密码");
            return;
        }
        if (StringUtils.isEmpty(idEtYZM.getText().toString())) {
            showToast("请输入验证码");
            return;
        }

//        if (StringUtils.isEmpty(idEdtCode.getText().toString())){
//            showToast("请输入验证码");
//            return;
//        }
        MyDialog myDialog = new MyDialog(mAc, this, "提示", "注销后所有用户数据将删除！");
        myDialog.show();
        ;

//        finish();
//        firstHttp();
    }

//    //第一次提交
//    private void firstHttp(){
//        HttpRequest.postUrl(Api.FORGET_PASSWORD_STP1)
//                .addParams("phone",idEdtPhone.getText().toString())
//                .addParams("UserName",idEdtZh.getText().toString())
//                .addParams("PhoneVerifyCode",idEdtCode.getText().toString())
//                .execute(new PostProcess.StringCallBack() {
//                    @Override
//                    public void onResponse(String json) {
//                        boolean checkCode = Utils.checkCode(mAc, json);
//                        if (checkCode){
//                            Gson gson = new Gson();
//                            ForgetFirstStpModel forgetFirstStpModel = gson.fromJson(json, ForgetFirstStpModel.class);
//                            ForgetPassStp2Activity.actionStart(mAc,forgetFirstStpModel.getData().getForget_password_token());
//                            finish();
//                        }
//                        showLog(json);
//                    }
//
//                    @Override
//                    public void onError(Exception e) {
//
//                    }
//                }
//                );
//    }


    @OnClick({R.id.id_btn_next, R.id.id_tv_time})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.id_btn_next://下一步
                isRight();
                break;
            case R.id.id_tv_time:
                ForgetCodeDialog dialog = new ForgetCodeDialog(mAc, UserName, Phone, countDownTimerUtils,this);
                dialog.show();
                break;
        }
    }

    private void check() {
        HttpRequest.postUrl(Api.ZHUXIAO)
                .addParams("UserName", idEdtZh.getText().toString())
                .addParams("password",idEdtPhone.getText().toString())
                .addParams("VerifyCode", idEtYZM.getText().toString())
                .addParams("phone",Phone)
                .executeNoToken(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkcode = Utils.checkCode(mAc, json);
                        if (checkcode) {
                            Utils.cleanBaseApplication();
                            ActivityCollector.finishAll();
                            intent2Activity(LoginActivity.class);
                            MyToast.showShortToast(mAc, "账号已注销");
                        }
                    }

                    @Override
                    public void onError(Exception e) {

                    }
                });
    }


    @Override
    public void RightClick() {
        check();
    }

    @Override
    public void CancelClick() {

    }

    @Override
    public void passPay(String pass) {
        cToken = pass;
    }
}
